//
//  GQHTransactionDetailView.m
//  Xanthium
//
//  Created by GuanQinghao on 28/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHTransactionDetailView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"

@interface GQHTransactionDetailView () <UITableViewDelegate,UITableViewDataSource, UITextFieldDelegate> {
    
    NSArray *captionArray;
    NSArray *_placeHolderArray;

}

@end

@implementation GQHTransactionDetailView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

- (void)loadLocalData {
    
    captionArray = @[@"转账日期",@"转出账号",@"账号户名",@"开户银行",@"转账币种",@"转账金额",@"转账备注"];
    _qh_contentArray = [NSMutableArray arrayWithArray:@[@"请选择",@"",@"",@"",@"人民币(¥)",@"",@""]];
    _placeHolderArray = @[@"请选择",@"请输入",@"请输入",@"请输入",@"人民币(¥)",@"请输入",@"请输入"];
}

- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return captionArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    YYEnterTextCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"YYEnterTextCell" owner:self options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = captionArray[indexPath.row];
    cell.enterText.placeholder = _placeHolderArray[indexPath.row];
    cell.enterText.text = [NSString stringWithFormat:@"%@", _qh_contentArray[indexPath.row]];
    cell.enterText.delegate = self;
    cell.enterText.tag = indexPath.row;
    cell.isEnter = YES;
    cell.perLabel_width.constant = 0;
    cell.enterText.enabled = self.qh_editable;
    
    if (indexPath.row == 0)
    {
        cell.isEnter = NO;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.showValueLabel.text = _qh_contentArray[indexPath.row];
    }
    if (indexPath.row == 4)
    {
        cell.enterText.enabled = NO;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {

        [tableView deselectRowAtIndexPath:indexPath animated:YES];

        if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {

            [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [_qh_contentArray replaceObjectAtIndex:textField.tag withObject:textField.text];
}

@end
